<?php
namespace backend\controllers;

use backend\models\Banner;
use backend\models\Project;
use backend\models\Team;
use common\components\BaseController;
use Symfony\Component\Yaml\Tests\B;
use Yii;
use common\helps\tools;
use common\helps\code;
/**
 * Project controller
 */
class BannerController extends BaseController
{

    public $modelClass = 'common\models\Banner';
    public function init()
    {
        parent::init();
    }

    /**
     * 获取团队介绍列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetBannerList(){
        $request =$this->request;
        $model = new Banner();
        $params = $request->queryParams;
        $total_data = $model->getBannerList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','跳转链接','状态','创建时间'];
            foreach($total_data as &$value){
                unset($value['pic']);
                if($value['status']==0){
                    $value['status'] ='停用';
                }else{
                    $value['status'] ='在用';
                }
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'Banner列表');
        }


        return code::success($data);

    }

    /**
     * 新增banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            $model->scenario='create';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('error',$error[0][0]);
            }
            $data=$model->addBannerInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }


    /**
     * 编辑banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            $model->scenario='update';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('error',$error[0][0]);
            }
            $data=$model->editBannerInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 删除banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDeleteBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            $model->scenario='update_status';
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('error',$error[0][0]);
            }
            $data=$model->deleteBannerInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 批量删除banner
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionBatchDelBannerInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Banner();
            $params = $request->bodyParams;
            if(empty($params['ids'])) return code::error('error','参数错误');
            $data=$model->batchDelBannerInfo($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }





}

